<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css" />
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field="nickname">姓名</th>
							<th class="text-center" data-field="idCard">身份证</th>
							<th class="text-center" data-field="tel">联系电话</th>
							<th class="text-center" data-field="s1">性别</th>
							<th class="text-center" data-field="identity">身份</th>
							<th class="text-center" data-field="isParty">政治面貌</th>
							<th class="text-center" data-field="createTime">创建时间</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">人员管理</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="nickname" class="control-label">姓名：</label> <input type="text" class="form-control" id="nickname">
					</div>
					<div class="form-group">
						<label for="idCard" class="control-label">身份证：</label> <input type="text" class="form-control" id="idCard">
					</div>
					<div class="form-group">
						<label for="tel" class="control-label">联系电话：</label> <input type="text" class="form-control" id="tel">
					</div>
					<div class="form-group">
						<label for="s1" class="control-label">性别：</label> <select class="form-control" id="s1">
							<option value="">请选择...</option>
							<option value="男">男</option>
							<option value="女">女</option>
						</select>
					</div>
					<div class="form-group">
						<label for="identity" class="control-label">身份：</label><select class="form-control" id="identity">
							<option value="">请选择...</option>
							<option value="业主">业主</option>
							<option value="租户">租户</option>
						</select>
					</div>
					<div class="form-group">
						<label for="isRetire" class="control-label">是否退休：</label> <select class="form-control" id="isRetire">
							<option value="">请选择...</option>
							<option value="否">否</option>
							<option value="是">是</option>
						</select>
					</div>
					<div class="form-group">
						<label for="addr" class="control-label">住址：</label> <input type="text" class="form-control" id="addr">
					</div>
					<div class="form-group">
						<label for="isParty" class="control-label">政治面貌：</label> <input type="text" class="form-control" id="isParty">
					</div>
					<!-- 					<div class="form-group"> -->
					<!-- 						<label for="post" class="control-label">岗位：</label> <input type="text" class="form-control" id="post"> -->
					<!-- 					</div> -->
				</div>
				<div class="modal-footer">
					<button id="submitBtn" type="button" class="btn btn-primary" onclick="insertPersonnel();">提交</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	var selectId = '';
	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-success btn-xs glyphicon glyphicon-file' onclick='showModal(\"" + row.id + "\");'>查看详情</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delPersonnel(\"" + row.id + "\")'>删除信息</button>",
				"&nbsp;<button class='btn btn-warning btn-xs glyphicon glyphicon-user' onclick='generateUser(\"" + row.id + "\")'>生成app用户</button>", ].join('');
	}

	var table_dataset = [ {
		field : 'nickname',
		align : 'center',
	}, {
		field : 'idCard',
		align : 'center',
	}, {
		field : 'tel',
		align : 'center',
	}, {
		field : 's1',
		align : 'center',
	}, {
		field : 'identity',
		align : 'center',
	}, {
		field : 'isParty',
		align : 'center',
	}, {
		field : 'createTime',
		align : 'center',
		formatter : function timeFormatter(value, row, index) {
			return (new Date(row.createTime)).Format("yyyy-MM-dd hh:mm:ss");
		}
	}, {
		align : 'center',
		width : 300,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable(
			{
				url : "${pageContext.request.contextPath}/selectPersonnels",
				idField : "sid",
				columns : table_dataset,
				striped : true, //是否显示行间隔色
				pagination : true, //是否显示分页
				pageSize : 10, //每页的记录行数
				pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
				search : false, //是否显示搜索
				showRefresh : true,
				sidePagination : "server", //表示服务端请求  
				queryParamsType : "limit",
				queryParams : function queryParams(params) { //设置查询参数  
					var param = {
						offset : params.offset,
						limit : params.limit,
						name : $("#name").val(),
						card : $("#card").val(),
						tel : $("#tel1").val()
					};
					return param;
				},
				toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>"
						+ "<div class='input-group' style='width:250px;float:right;margin-top:2px;margin-left:20px;'>"
						+ "<input type='text' class='form-control' placeholder='请输入手机号进行查询...' id='tel1' name='tel1'>"
						+ "<span class='input-group-btn'><button class='btn btn-default' type='button' onclick='querry();'>查询</button></span></div>"
						+ "<div class='input-group' style='width:250px;float:right;margin-top:2px;margin-left:20px;'>"
						+ "<input type='text' class='form-control' placeholder='请输入身份证进行查询...' id='card' name='card'>"
						+ "<span class='input-group-btn'><button class='btn btn-default' type='button' onclick='querry();'>查询</button></span></div>"
						+ "<div class='input-group' style='width:250px;float:right;margin-top:2px;margin-left:20px;'>"
						+ "<input type='text' class='form-control' placeholder='请输入姓名进行查询...' id='name' name='name'>"
						+ "<span class='input-group-btn'><button class='btn btn-default' type='button' onclick='querry();'>查询</button></span></div>",
				icons : {
					paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
					paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
					refresh : 'glyphicon-refresh icon-refresh',
					toggle : 'glyphicon-list-alt icon-list-alt',
					columns : 'glyphicon-th icon-th',
					detailOpen : 'glyphicon-plus icon-plus',
					detailClose : 'glyphicon-minus icon-minus',
					"export" : 'glyphicon-export icon-share'
				},
				showExport : true,
				exportDataType : 'basic',
				exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ],
			});

	function showModal(id) {
		selectId = id;
		if (id == '') {
			$("#nickname").val('');
			$("#idCard").val('');
			$("#tel").val('');
			$("#s1").val('');
			$("#identity").val('');
			$("#isRetire").val('');
			$("#addr").val('');
			$("#isParty").val('');
			// 			$("#post").val('');
		} else {
			$.ajax({
				url : "${pageContext.request.contextPath}/selectPersonnelById",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(map) {
					var personnel = map['personnel'];
					$("#nickname").val(personnel.nickname);
					$("#idCard").val(personnel.idCard);
					$("#tel").val(personnel.tel);
					$("#s1").val(personnel.s1);
					$("#identity").val(personnel.identity);
					$("#isRetire").val(personnel.isRetire);
					$("#addr").val(personnel.addr);
					$("#isParty").val(personnel.isParty);
					// 					$("#post").val(personnel.post);
				}
			});
		}
		$('#modal').modal({
			backdrop : 'static',
			keyboard : false
		});
	}

	function insertPersonnel() {
		$("#submitBtn").button('loading');
		var nickname = $("#nickname").val();
		var idCard = $("#idCard").val();
		var tel = $("#tel").val();
		var s1 = $("#s1").val();
		var identity = $("#identity").val();
		var isRetire = $("#isRetire").val();
		var addr = $("#addr").val();
		var isParty = $("#isParty").val();
		var post = $("#post").val();
		if (nickname === '' || idCard === '' || tel === '') {
			layer.alert("请填写姓名，身份证，联系方式!");
			$("#submitBtn").button('reset');
			return;
		}
		if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(tel))) {
			layer.alert("联系电话格式不正确！");
			$("#submitBtn").button('reset');
			return;
		}
		if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idCard))) {
			layer.alert("身份证号填写有误！");
			$("#submitBtn").button('reset');
			return;
		}
		$.ajax({
			url : "${pageContext.request.contextPath}/insertAndUpdatePersonnel",
			type : "post",
			data : {
				id : selectId,
				nickname : nickname,
				idCard : idCard,
				tel : tel,
				s1 : s1,
				identity : identity,
				isRetire : isRetire,
				addr : addr,
				isParty : isParty,
				post : post
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result == 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
				$("#submitBtn").button('reset');
			}
		});
	}

	function delPersonnel(id) {
		layer.confirm("是否删除此项？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/insertAndUpdatePersonnel",
				type : "post",
				data : {
					id : id,
					status : '-1'
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}

	function generateUser(id) {
		layer.confirm("是否生成app用户？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/generateUser",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else if (data.result === 2) {
						layer.msg('此用户已有app账号!', {
							icon : 2,
							time : 800
						});
					} else if (data.result === 3) {
						layer.msg('此用户无房产信息!', {
							icon : 2,
							time : 800
						});
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}

	function querry() {
		$("#table").bootstrapTable('refresh');
	}
</script>
</html>
